package com.smfx.yxt.controller.teacher;

import java.util.Calendar;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smfx.yxt.common.AppUser;
import com.smfx.yxt.common.Const;
import com.smfx.yxt.common.RspResult;
import com.smfx.yxt.common.enums.Status;
import com.smfx.yxt.controller.BaseController;
import com.smfx.yxt.model.CardSign;
import com.smfx.yxt.service.ICardSignService;
import com.smfx.yxt.utils.DateUtil;
import com.smfx.yxt.utils.RequestUtil;
import com.smfx.yxt.utils.StringUtil;

@RequestMapping(Const.TEACHER_ROLE + "/work")
@Controller
public class TeacherWorkController extends BaseController{
	
	private Logger logger = LoggerFactory.getLogger(TeacherWorkController.class);
	
	private static String TEACHER_AM_START_DATE = " 6:30";//教师上午打卡开始
	private static String TEACHER_AM_END_DATE = " 8:00";//教师上午打卡结束
	private static String OFFICE_AM_START_DATE = " 6:30";//职员上午打卡开始
	private static String OFFICE_AM_END_DATE = " 7:50";//职员上午打卡结束
	
	private static String TEACHER_PM_START_DATE = " 13:30";//教师下午打卡开始
	private static String TEACHER_PM_END_DATE = " 14:20";//教师下午打卡结束
	private static String FRIDAY_TEACHER_PM_START_DATE = " 13:10";//周五下午打卡开始
	private static String FRIDAY_TEACHER_PM_END_DATE = " 14:20";//周五下午打卡结束
	
	private static String SUNDAY_CLASS_PM_START_DATE = " 17:00";//周日班主任下午打卡开始
	private static String SUNDAY_CLASS_PM_END_DATE = " 18:00";//周日班主任下午打卡开始

	@Autowired
	private ICardSignService cardSignService;
	
	@RequestMapping("cardSign")
	public String cardSign(Model model) {
		String ip = RequestUtil.getIpAddr(request);
		if(ip.startsWith("10.19.31") || ip.startsWith("10.19.34")) {
			model.addAttribute("isSchool", true);
		}
		return "work/cardSign";
	}
	
	@RequestMapping("today")
	@ResponseBody Object todaySign() {
		AppUser appUser = getAppUser();
		CardSign record = new CardSign();
		record.setTeacherId(appUser.getTeacher().getId());
		return cardSignService.selectListForToday(record);
	}
	
	@RequestMapping("cardSign/submit")
	@ResponseBody Object cardSignSubmit() {
		try {
			AppUser appUser = getAppUser();
			Date signTime = Calendar.getInstance().getTime();
			Long currTime = Calendar.getInstance().getTimeInMillis();
			String currDate = DateUtil.dateToStr(new Date(), 
					DateUtil.DEFAULT_DATE_FORMAT3);
			Long startDate = null;
			Long endDate = null;
			int signType = 0;
			String ip = RequestUtil.getIpAddr(request);
			if (DateUtil.isAmOrPm() == 0) signType = 1;
			else signType = 2;
			if (signType == 1) {
				boolean isTeacher = StringUtil.isExist(appUser.getTeacher().getPosition(), "3006");
				startDate = DateUtil.strToDate(currDate + TEACHER_AM_START_DATE, 
						DateUtil.DEFAULT_DATE_FORMAT1).getTime();
				endDate = DateUtil.strToDate(currDate + TEACHER_AM_END_DATE, 
						DateUtil.DEFAULT_DATE_FORMAT1).getTime();
				if(!isTeacher) {
					startDate = DateUtil.strToDate(currDate + OFFICE_AM_START_DATE, 
							DateUtil.DEFAULT_DATE_FORMAT1).getTime();
					endDate = DateUtil.strToDate(currDate + OFFICE_AM_END_DATE, 
							DateUtil.DEFAULT_DATE_FORMAT1).getTime();
				}
			} else {
				if(DateUtil.dayForWeek() == 5) {
					startDate = DateUtil.strToDate(currDate + FRIDAY_TEACHER_PM_START_DATE, 
							DateUtil.DEFAULT_DATE_FORMAT1).getTime();
					endDate = DateUtil.strToDate(currDate + FRIDAY_TEACHER_PM_END_DATE, 
							DateUtil.DEFAULT_DATE_FORMAT1).getTime();
				}else if(DateUtil.dayForWeek() == 7) {
					startDate = DateUtil.strToDate(currDate + SUNDAY_CLASS_PM_START_DATE, 
							DateUtil.DEFAULT_DATE_FORMAT1).getTime();
					endDate = DateUtil.strToDate(currDate + SUNDAY_CLASS_PM_END_DATE, 
							DateUtil.DEFAULT_DATE_FORMAT1).getTime();
				}else {
					startDate = DateUtil.strToDate(currDate + TEACHER_PM_START_DATE, 
							DateUtil.DEFAULT_DATE_FORMAT1).getTime();
					endDate = DateUtil.strToDate(currDate + TEACHER_PM_END_DATE, 
							DateUtil.DEFAULT_DATE_FORMAT1).getTime();
				}
			}
			if(currTime >= startDate && currTime <= endDate) {
				CardSign record = new CardSign();
				record.setTeacherId(appUser.getTeacher().getId());
				record.setSignType(signType);
				int count = cardSignService.selectCountForToday(record);
				if(count > 0) {
					return new RspResult(Status.CARD_SIGN_SUCESS);
				}
				CardSign cardSign = new CardSign();
				cardSign.setSignType(signType);
				cardSign.setRemark(ip);
				cardSign.setTeacherId(appUser.getTeacher().getId());
				cardSign.setSignTime(signTime);
				cardSignService.insertSelective(cardSign);
				return new RspResult(Status.SUCCESS);
			}
			return new RspResult(Status.CARD_SIGN_TIMEOUT);
		} catch (Exception e) {
			logger.error("--cardSignSubmit error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
}
